//
//  SNBpChartView.m
//  project
//
//  Created by 黄建华 on 2017/12/8.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "SNBpChartView.h"
#import "SNBpView.h"

#define LEFTWIDTH        30.0
#define SNHEARTCOLORDEEP [UIColor colorFromHex:0xf41628]
@implementation SNBpChartView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
     [self performSelector:@selector(createChartView) withObject:nil afterDelay:IOSINITWAITTIME];
    }
    return self;
}

- (void)createChartView {
    self.backgroundColor = [UIColor whiteColor];
}

- (void)UpdateViewWithType:(SelectDateType)type withArray:(NSArray *)dataArray {
    [self loadBackgroundLayer];
    //数据加载
    NSMutableArray *array = [NSMutableArray array];
    
    if (type == 0) {
        array = [NSMutableArray arrayWithArray:dataArray];
    } else if (type == 1) {
        array = [NSMutableArray arrayWithArray:dataArray];
    } else if (type == 2) {
        array = [NSMutableArray arrayWithArray:dataArray];
    } else if (type == 3) {
        for (int i = 0; i < 12; i++) {
            int          max  = arc4random()%100+100;
            int          min  = arc4random()%100;
            NSDictionary *dic = @{@"SBP":StrByInt(max), @"DBP":StrByInt(min), @"SPACE":StrByInt(max - min)};
            [array addObject:dic];
        }
    }
    
//    NSLog(@"array>>>>>>%@",array);
    
    _selectDateLabel               = [[SNCustomLabel alloc] initWithFrame:CGRectMake(-20, 5, 200, 20)];
    _selectDateLabel.textAlignment = NSTextAlignmentLeft;
    _selectDateLabel.textColor     = SNCOLORBLACK;
    _selectDateLabel.font          = SNSFONT(14.0);
    [_chartView addSubview:_selectDateLabel];
    
    //    _valueLabel = [[SNCustomLabel alloc] initWithFrame:CGRectMake(HHHWIDTH - 205, 10, 200, 20)];
    //    _valueLabel.textAlignment = NSTextAlignmentRight;
    //    _valueLabel.textColor = SNCOLORBLACK;
    //    _valueLabel.font = SNSFONT(13.0);
    //    [_chartView addSubview:_valueLabel];
    //    _valueLabel.text = [NSString stringWithFormat:@"%@    %ld  ",SN_Text(@"Total steps:"),(long)model.step];
    
    _unitLabel               = [[SNCustomLabel alloc] initWithFrame:CGRectMake(10, self.height - 30, self.width - 20, 15)];
    _unitLabel.textColor     = SNCOLORGRAY;
    _unitLabel.font          = SNFONT(10.0);
    _unitLabel.textAlignment = NSTextAlignmentLeft;
    [_chartView addSubview:_unitLabel];
    
    UIView *lineGray0 = [[UIView alloc] init];
    lineGray0.backgroundColor = SNCOLORWITHHEX(0xd7d8d9);
    lineGray0.frame           = SNFRAME(5, _chartView.origin.y + 50, 1, _chartView.height - 90);
    [_chartView addSubview:lineGray0];
    
    NSArray *qualityArray = [[NSArray alloc] initWithObjects:@"0", @"50", @"100", @"150", @"200", nil];
    for (int i = 0; i < 5; i++) {
        SNCustomLabel *label = [[SNCustomLabel alloc] initWithFrame:CGRectMake(-LEFTWIDTH -3, _chartView.origin.y + 41 + i*(_chartView.height - 90 + FitSimpleSize(15, 20, 30, 35, 45))/5, 30, 20)];
        label.textAlignment = NSTextAlignmentRight;
        label.textColor     = SNCOLORBLACK;
        label.font          = SNSFONT(9.5);
        label.text          = [qualityArray objectAtIndex:4 -i];
        [_chartView addSubview:label];
        if (i < 4) {
            UIView *line = [[UIView alloc] init];
            line.frame           = SNFRAME(5, label.center.y, self.width - 10, 0.5);
            line.backgroundColor = SNCOLORWITHHEX(0xd7d8d9);
            line.alpha           = 0.33;
            [_chartView addSubview:line];
        }
    }
    
    UIView *lineGray = [[UIView alloc] init];
    lineGray.backgroundColor = SNCOLORWITHHEX(0xd7d8d9);
    lineGray.frame           = SNFRAME(5, self.height - 40, self.width - 10, 1);
    [_chartView addSubview:lineGray];
    
    if (type == 0) {
        _selectDateLabel.text = [SN_PublicClass.selectDate dateToSelectDateShow];
    } else {
        _selectDateLabel.text = [SN_PublicClass showDateWithType:type];
    }
    
    NSInteger maxValue = 200;
    NSInteger minValue = 0;
    
    float   Origin_x      = 10;
    CGFloat Origin_height = self.height - 40;
    CGFloat offsetY       = FitScreenNumber(40, 40, 40, 40, 40);
    float   max           = 0;
    
    for (int i = 0; i < array.count; i++) {
        NSDictionary *dic = [array objectAtIndex:i];
        
        CGFloat value       = [[dic valueForKey:@"DBP"]floatValue];
        CGFloat value2       = [[dic valueForKey:@"SBP"]floatValue];
        
        
        
        CGFloat valuelength = [[dic valueForKey:@"SPACE"]floatValue];
        if (value > maxValue) {
            value = maxValue;
        }
        
        CGFloat height = 0;
        CGFloat h      = (self.height - (25 + 30 + offsetY)) * (value / maxValue);
        if (isnan(h)) {
            height = 0;
        } else {
            height = h;
        }
        static int total = 0;
        total = total + value;
        
        CGFloat chartWidth = 0;
        CGFloat chartSpace = 0;
        if (type == 0) {
            chartWidth = 2.5;
            chartSpace = (self.width - Origin_x *2)/array.count;
        } else if (type == 1) {
            Origin_x   = 20;
            chartWidth = 5;
            chartSpace = (self.width - Origin_x *2)/7 + 5;
        } else if (type == 2) {
            chartWidth = 3;
            chartSpace = (self.width - Origin_x *2)/array.count;
        } else if (type == 3) {
            chartWidth = 3;
            chartSpace = (self.width - Origin_x *2)/12 + 1.5;
        }
        
        CGPoint point01 = CGPointMake(i * chartSpace + Origin_x - LEFTWIDTH, Origin_height - height);
        CGPoint point02 = CGPointMake((i) * chartSpace + Origin_x +chartWidth - LEFTWIDTH, Origin_height - height);
        
        CGFloat valueHeight = 0.0;
        valueHeight = valuelength /200 * (self.height - (25 + 30 + offsetY));
        // 血压View
        if (valuelength > 0) {
            SNBpView *bpView = [[SNBpView alloc] initWithFrame:SNFRAME(point01.x + LEFTWIDTH, point01.y -2, chartWidth, valueHeight)];
            [_chartView addSubview:bpView];
        }
        
        //圆角
        UIView *view = [[UIView alloc] init];
        view.frame = SNFRAME(point01.x + LEFTWIDTH, point01.y + height, chartWidth, height == 0 ? 0 : 5);
        
        if (type == 0) {
            NSArray       *weekArray = [[NSArray alloc] initWithObjects:@"00:00", @"04:00", @"08:00", @"12:00", @"16:00", @"20:00", @"23:59", nil];
            SNCustomLabel *label = [[SNCustomLabel alloc] initWithFrame:CGRectMake(0, self.height - 30, 100, 20)];
            label.center        = CGPointMake(view.center.x +   2.0, self.height - 25);
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor     = SNCOLORBLACK;
            label.font          = SNSFONT(9.0);
            [_chartView addSubview:label];
            int num = 0;
            if (array.count == 48) {
                num = 8;
            }
            if (array.count == 96) {
                num = 16;
            }
            if (i % num == 0) {
                label.text = [weekArray objectAtIndex:i/num];
            }
            
            if (i == array.count - 1) {
                label.text = [weekArray objectAtIndex:6];
                label.center  = CGPointMake(view.center.x -   2.0, self.height - 25);
            }
        }
        
        if (type == 1) {
            NSArray       *weekArray = [[NSArray alloc] initWithObjects:SN_Text(@"周日"), SN_Text(@"周一"), SN_Text(@"周二"), SN_Text(@"周三"), SN_Text(@"周四"), SN_Text(@"周五"), SN_Text(@"周六"), nil];
            SNCustomLabel *label     = [[SNCustomLabel alloc] initWithFrame:CGRectMake(0, self.height - 30, 100, 20)];
            label.center        = CGPointMake(view.center.x, self.height - 25);
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor     = SNCOLORBLACK;
            label.font          = SNSFONT(10.0);
            label.text          = [weekArray objectAtIndex:i];
            [_chartView addSubview:label];
        }
        if (type == 2) {
            SNCustomLabel *label = [[SNCustomLabel alloc] initWithFrame:CGRectMake(0, self.height - 30, 100, 20)];
            label.center        = CGPointMake(view.center.x, self.height - 25);
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor     = SNCOLORBLACK;
            label.font          = SNSFONT(10.0);
            [_chartView addSubview:label];
            if (i == 0 || i == 4 || i == 9 || i == 14 || i == 19 || i == 24 || i == 29) {
                label.text = StrByInt(i+1);
            } else {
                label.text = @".";
            }
        }
        //        if (type == 3)
        //        {
        //            SNCustomLabel *label = [[SNCustomLabel alloc] initWithFrame:CGRectMake(0, self.height - 30, 100, 20)];
        //            label.center = CGPointMake(view.center.x, self.height - 25);
        //            label.textAlignment = NSTextAlignmentCenter;
        //            label.textColor = SNCOLORBLACK;
        //            label.font = SNSFONT(10.0);
        //            label.text = StrByInt(i+1);;
        //            [_chartView addSubview:label];
        //        }
    }
    
    CGFloat height = 20;
    
    UIView *img1 = [[UIView alloc] init];
    img1.backgroundColor = [UIColor colorFromHex:0xe66490];
    img1.frame           = SNFRAME(HHHWIDTH - 105, 5+height, 25, 2);
    [_chartView addSubview:img1];
    
    UIView *sbpDot = [[UIView alloc] init];
    sbpDot.frame               = SNFRAME(25, -1.25, 5, 5);
    sbpDot.backgroundColor     = [UIColor colorFromHex:0xe66490];
    sbpDot.layer.cornerRadius  = 2.5;
    sbpDot.layer.masksToBounds = YES;
    [img1 addSubview:sbpDot];
    
    UILabel *label1 = [[UILabel alloc] init];
    label1.frame         = SNFRAME(35, -9, 70, 20);
    label1.font          = SNFONT(9.0);
    label1.text          = SN_Text(@"收缩压");
    label1.textAlignment = NSTextAlignmentLeft;
    [img1 addSubview:label1];
    
    UIView *img2 = [[UIView alloc] init];
    img2.backgroundColor = [UIColor colorFromHex:0x65d4fe];
    img2.frame           = SNFRAME(HHHWIDTH - 105, 5 +15+height, 25, 2);
    [_chartView addSubview:img2];
    
    UIView *dbpDot = [[UIView alloc] init];
    dbpDot.frame               = SNFRAME(25, -1.25, 5, 5);
    dbpDot.backgroundColor     = [UIColor colorFromHex:0x65d4fe];
    dbpDot.layer.cornerRadius  = 2.5;
    dbpDot.layer.masksToBounds = YES;
    [img2 addSubview:dbpDot];
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.frame         = SNFRAME(35, -9, 70, 20);
    label2.font          = SNFONT(9.0);
    label2.text          = SN_Text(@"舒张压");
    label2.textAlignment = NSTextAlignmentLeft;
    [img2 addSubview:label2];
}

- (void)loadBackgroundLayer {
    if (_chartView) {
        [_chartView removeFromSuperview];
        _chartView = nil;
    }
    
    _chartView                 = [[UIView alloc] init];
    _chartView.backgroundColor = [UIColor whiteColor];
    _chartView.frame           = CGRectMake(0, 0, self.width, self.height);
    [self addSubview:_chartView];
    
    if (_backGroundLayer) {
        [_backGroundLayer removeFromSuperlayer];
        _backGroundLayer = nil;
    }
    _backGroundLayer                 = [CALayer layer];
    _backGroundLayer.backgroundColor = [UIColor clearColor].CGColor;
    _backGroundLayer.bounds          = CGRectMake(0, 0, self.width, self.height);
    _backGroundLayer.position        = CGPointMake(self.center.x, self.height / 2);
    [_chartView.layer addSublayer:_backGroundLayer];
}

- (NSInteger)max:(NSArray *)Array {
    if (Array.count == 0) {
        return 0;
    }
    
    NSInteger max = [[Array objectAtIndex:0] integerValue];
    
    for (int i = 0; i < Array.count; i++) {
        if ([[Array objectAtIndex:i] integerValue] > max) {
            max = [[Array objectAtIndex:i] integerValue];
        }
    }
    
    return max;
}

- (NSInteger)min:(NSArray *)Array {
    if (Array.count == 0) {
        return 0;
    }
    
    NSInteger min = [[Array objectAtIndex:0] integerValue];
    
    for (int i = 0; i < Array.count; i++) {
        if ([[Array objectAtIndex:i] integerValue] < min) {
            min = [[Array objectAtIndex:i] integerValue];
        }
    }
    return min;
}

@end

